package com.matrix.media.controller;

import com.matrix.common.entity.Result;
import com.matrix.common.entity.ResultCode;
import com.matrix.media.dto.MediaDTO;
import com.matrix.media.entity.Audio.AudioTrack;
import com.matrix.media.entity.Audio.AudioTrackClip;
import com.matrix.media.entity.VideoTrack;
import com.matrix.media.entity.VideoTrackClip;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

@CrossOrigin
@RestController
@RequestMapping("/matrix/media")
public class MediaController {


    /**
     * 产生随机视频的几种方式
     *
     *
     * 1、限制最大时长MaxDuration
     * 2、4个视频素材长度20s， 音频15s， 设置音频主轨道
     * 3、轨道间素材对齐： https://help.aliyun.com/document_detail/2507542.html?spm=a2c4g.451633.0.0.6f7a430dtF4P75
     *   3.1、您可以使用ClipId和ReferenceClipId两个参数，来指定素材间的对齐与被对齐关系。
     * 4、随机滤镜
     * 5、随机效果： https://help.aliyun.com/document_detail/451633.html?spm=a2c4g.451632.0.0.7e807e69Ihjkaf
     *
     */









    /**
     * 高效产生随机视频
     * @param map
     * @return
     */
    @PostMapping("/media")
    public Result media(@RequestBody Map<String, Object> map) {

        //生成视频个数
        Integer count = (Integer) map.get("count");
        String musicURL = (String) map.get("bgMusic");
        List<Map<String, Object>> array = (List<Map<String, Object>>) map.get("list");

        Random rand = new Random();

        List<MediaDTO> mediaDTOList = new ArrayList<>();

        //生产10个视频，循环10次，每次生成1个视频
        for (int i = 1; i <= count; i++) {

            MediaDTO mediaDTO = new MediaDTO();

            StringBuilder sb = new StringBuilder();
            VideoTrack tracks = new VideoTrack();

            List<VideoTrackClip> clips = new ArrayList<>();

            for (Map<String, Object> item : array) {
                //随机文案
                List<String> contents = (List<String>)item.get("contents");
                int randomContentNum = rand.nextInt(contents.size());
                String tmpContent = contents.get(randomContentNum);
                sb.append(tmpContent);

                //随机视频
                List<String> videos = (List<String>)item.get("videos");
                int randomVideosNum = rand.nextInt(videos.size());
                String videoUrl = videos.get(randomVideosNum);

                VideoTrackClip trackClip = new VideoTrackClip();
                trackClip.setMediaURL(videoUrl);

                clips.add(trackClip);
            }

            tracks.setVideoTrackClips(clips);

            AudioTrack audioTrack = new AudioTrack();
            List<AudioTrackClip> audios = new ArrayList<>();

            AudioTrackClip audioTrackClip = new AudioTrackClip();
//            audioTrackClip.setMediaURL(musicURL);
            audios.add(audioTrackClip);

            audioTrack.setAudioTrackClips(audios);

            //设置音频
            mediaDTO.setAudioTrack(audioTrack);

            //设置内容
            mediaDTO.setContent(sb.toString());

            //设置视频
            mediaDTO.setVideoTrack(tracks);

            mediaDTOList.add(mediaDTO);
        }

        System.out.println(mediaDTOList);

        return Result.success();
    }
}
